Skip to content

fix: bug where query clone params are assigned to claim params#2184

Merged
SkArchon merged 1 commit intomainfrom
milinda/expr-bug-resolving
Sep 3, 2025
Merged

fix: bug where query clone params are assigned to claim params#2184
SkArchon merged 1 commit intomainfrom
milinda/expr-bug-resolving

Conversation

@SkArchon
Copy link
Copy Markdown
Contributor

@SkArchon SkArchon commented Sep 2, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Cloned request contexts now correctly retain URL query parameters.
    • Changes made to data in a cloned context no longer affect the original, improving reliability across middleware and handlers.
    • Ensures empty or missing maps/slices in the original context are safely initialized in clones.
  • Tests

    • Added comprehensive tests covering cloning behavior for query parameters, claims, scopes, and handling of nil/empty collections to prevent regressions.

Checklist

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Sep 2, 2025

Walkthrough

Fixes Context.Clone() to correctly copy URL.Query into the cloned context. Adds comprehensive tests covering cloning behavior, including handling of nil/empty maps and slices, separation of mutable state, and verification that modifying the clone does not affect the original.

Changes

Cohort / File(s) Summary of modifications
Clone bug fix
router/internal/expr/expr.go
Corrects assignment in Context.Clone(): copies URL.Query entries into the new query map instead of the claims map.
Clone behavior tests
router/internal/expr/expr_test.go
Adds subtests for query cloning, nil/empty maps and slices, copy semantics for Claims/Scopes/URL.Query, and mutation isolation between original and clone; minor import reordering.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions Bot added the router label Sep 2, 2025
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Sep 2, 2025

Router image scan passed

✅ No security vulnerabilities found in image:

ghcr.io/wundergraph/cosmo/router:sha-354b4f9211e1689cca75bda0c0fe09f68f14d88d

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
router/internal/expr/expr.go (1)

40-47: Clarify cloning semantics: not a true deep copy.

Clone duplicates top-level maps/slices but intentionally does not deep-copy nested values (e.g., map/slice entries inside Claims). The comment “deep copy” is misleading.

Apply:

-// Clone creates a deep copy of the Context
+// Clone returns a copy of Context with fresh top-level maps/slices (Auth.Claims, Auth.Scopes, URL.Query).
+// Nested reference values inside Claims are not deep-copied by design. Nil inputs become non-nil, empty.
 func (copyCtx Context) Clone() *Context {
router/internal/expr/expr_test.go (2)

60-80: Good regression for URL.Query cloning; optional extra assertion.

Consider also asserting map instance inequality for consistency with the earlier pointer-based checks.

 		clone := exprContext.Clone()
 
 		require.Equal(t, exprContext.Request.URL.Query, clone.Request.URL.Query)
 
 		// Verify modifying clone doesn't affect original
 		clone.Request.URL.Query["new"] = "value2"
 		require.NotEqual(t, exprContext.Request.URL.Query, clone.Request.URL.Query)
+		// Also ensure different map instances
+		require.NotEqual(t,
+			reflect.ValueOf(exprContext.Request.URL.Query).Pointer(),
+			reflect.ValueOf(clone.Request.URL.Query).Pointer(),
+		)

132-178: Make shallow-copy intent explicit by mutating nested values in the test.

You already assert pointer equality for nested slice/map in Claims. Add a quick mutation to document the shared-reference behavior.

 		sliceFromOriginal := exprContext.Request.Auth.Claims["slice"].([]string)
 		sliceFromClone := clone.Request.Auth.Claims["slice"].([]string)
 		require.Equal(t, reflect.ValueOf(sliceFromOriginal).Pointer(), reflect.ValueOf(sliceFromClone).Pointer())
 
 		mapFromOriginal := exprContext.Request.Auth.Claims["map"].(map[string]string)
 		mapFromClone := clone.Request.Auth.Claims["map"].(map[string]string)
 		require.Equal(t, reflect.ValueOf(mapFromOriginal).Pointer(), reflect.ValueOf(mapFromClone).Pointer())
+
+		// Mutate via clone to make the contract explicit
+		sliceFromClone[0] = "x"
+		mapFromClone["key"] = "changed"
+		require.Equal(t, "x", sliceFromOriginal[0])
+		require.Equal(t, "changed", mapFromOriginal["key"])
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 0f15d1e and 9031972.

📒 Files selected for processing (2)
  • router/internal/expr/expr.go (1 hunks)
  • router/internal/expr/expr_test.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
router/internal/expr/expr_test.go (1)
router/internal/expr/expr.go (4)
  • Context (34-38)
  • Request (65-74)
  • RequestURL (102-112)
  • RequestAuth (118-123)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: build-router
  • GitHub Check: integration_test (./. ./fuzzquery ./lifecycle ./modules)
  • GitHub Check: integration_test (./events)
  • GitHub Check: integration_test (./telemetry)
  • GitHub Check: image_scan (nonroot)
  • GitHub Check: image_scan
  • GitHub Check: build_push_image (nonroot)
  • GitHub Check: build_push_image
  • GitHub Check: build_test
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Analyze (go)
🔇 Additional comments (5)
router/internal/expr/expr.go (1)

56-56: Bug fix is correct: query values now copied into the query map.

This resolves the misassignment to claims during cloning and restores independent URL.Query state in clones.

router/internal/expr/expr_test.go (4)

6-7: Import change LGTM.


82-105: Empty maps/slices behavior covered well.


107-130: Nil-to-empty contract verified cleanly.


180-209: Separation of top-level mutable state verified.

Copy link
Copy Markdown
Contributor

@StarpTech StarpTech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@SkArchon SkArchon merged commit 6375c4b into main Sep 3, 2025
28 checks passed
@SkArchon SkArchon deleted the milinda/expr-bug-resolving branch September 3, 2025 08:59
@Noroth Noroth mentioned this pull request Sep 30, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants